//
//  UIImageView+DownloadImage.swift
//  StoreSearch
//
//  Created by 涂晶 on 16/6/3.
//  Copyright © 2016年 TuJing. All rights reserved.
//

import UIKit
import Alamofire

extension UIImageView {
    
    func loadImage(url: NSURL) {
        Alamofire.request(.GET,
                          url)
        .validate()
            .response {_, _, data, error in
                guard let image = UIImage(data: data!) else {
                    print("Error: \(error)")
                    return
                }
                
                dispatch_async(dispatch_get_main_queue(), {
                    self.image = image
                })
        }
    }
    
    /*func loadImageWithURL(url: NSURL) -> NSURLSessionDownloadTask
    {
        let session = NSURLSession.sharedSession()
        
        let downloadTask = session.downloadTaskWithURL(url, completionHandler: {
            [weak self] url, response, error in
            if error == nil, let url = url, data = NSData(contentsOfURL: url), image = UIImage(data: data) {
                dispatch_async(dispatch_get_main_queue()) {
                    if let strongSelf = self {
                        strongSelf.image = image
                    }
                }
            }
        })
        downloadTask.resume()
        return downloadTask
    }*/
}
